package com.xilihui.xlh.business.activitys;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tbruyelle.rxpermissions.RxPermissions;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.JoinStoreEntity;
import com.xilihui.xlh.business.requests.JoinShopRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.component.recyclerview.LinearLayoutColorDivider;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.app.ToolBaseCompatActivity;
import com.xilihui.xlh.core.exception.DefaultExceptionListener;
import com.xilihui.xlh.core.exception.ExceptionLayout;
import com.xilihui.xlh.core.exception.ExceptionManager;
import com.xilihui.xlh.core.glide.ImageHelper;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.ToastUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import rx.functions.Action1;

/**
 * Created by Administrator on 2018/5/17.
 */

public class JoinStoresActivity extends ToolBaseCompatActivity implements XRecyclerView.LoadingListener,View.OnClickListener, OnRefreshListener, OnLoadMoreListener {
    
    
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerview)
    XRecyclerView recyclerView;
    
    @BindView(R.id.tv_)
    TextView tv_;
    
    TextView tv_hint;
    Banner banner;
    TextView tv_shopName;
    TextView tv_intro;
    TextView tv_time;
    TextView tv_address;
    
    ExceptionManager exceptionManager;
    
    ArrayList<JoinStoreEntity.DataBean.AgentImagesBean> images = new ArrayList<>();
    ArrayList<JoinStoreEntity.DataBean.NoticeBean> notices = new ArrayList<>();
    BaseAdapter<JoinStoreEntity.DataBean.NoticeBean> baseAdapter;
    
    int page = 1;
    
    @Override
    public int getLayout() {
        return R.layout.activity_join_stores;
    }

    @Override
    public String initTitle() {
        return null;
    }
    
    @OnClick(R.id.iv_toolbar_left)
    public void back(){
        finish();
    }
    
    @OnClick(R.id.iv_toolbar_right)
    public void RQCode(){
        new RxPermissions(JoinStoresActivity.this)
                .request(Manifest.permission.CAMERA)
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean aBoolean) {
                        if(aBoolean){
                            Intent intent = new Intent(JoinStoresActivity.this,ScanActivity.class);
                            intent.putExtra("type",2);
                            startActivity(intent);
                            finish();
                        }else {
                            ToastUtil.toastShortNegative("您还未开启访问摄像头权限，请到应用管理中开启");
                        }
                    }
                });
         
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        
        setToolTitle("进入门店");
        setToolRightImage(R.mipmap.btn_flicking);
        tv_.setText("您最多可以加入一个门店\n点击右上角,扫一扫门店二维码更换门店");
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadMoreListener(this);
        exceptionManager = new ExceptionManager(smartRefreshLayout,new DefaultExceptionListener(this)){
            @Override
            protected int getEmptyView() {
                return R.layout.exception_join_stores_empty;
            }
        };
        
        exceptionManager.showEmpty();
        
        initHeadView();
        
        
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setLoadingMoreEnabled(false);
        recyclerView.addItemDecoration(new LinearLayoutColorDivider(getResources(),R.color.view_line,
                R.dimen.divider_line, LinearLayout.VERTICAL));
        
        baseAdapter = new BaseAdapter<JoinStoreEntity.DataBean.NoticeBean>(this,notices) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_shop_notification;
            }

            @Override
            protected void getView(BaseViewHolder holder, JoinStoreEntity.DataBean.NoticeBean entity, int position) {
                holder.setText(R.id.tv_title,entity.getTitle());
                holder.setText(R.id.tv_time,entity.getAdd_time());
                holder.setText(R.id.tv_content,entity.getContent());
            }
        };
        
        
        recyclerView.setAdapter(baseAdapter);
        recyclerView.setLoadingListener(this);
        
    }

    @Override
    public void initData() {
        getData(true);
    }
    
    public void initHeadView(){
        View view = LayoutInflater.from(JoinStoresActivity.this).inflate(R.layout.recycler_item_shop_head,
                null);
        tv_hint = (TextView)view.findViewById(R.id.tv_hint);
        tv_hint.setVisibility(View.VISIBLE);
        banner = (Banner)view.findViewById(R.id.banner_shop_img);
        banner.setBannerStyle(BannerConfig.NUM_INDICATOR);
        banner.setIndicatorGravity(Gravity.RIGHT);
        tv_shopName = (TextView)view.findViewById(R.id.tv_name);
        tv_intro = (TextView)view.findViewById(R.id.tv_intro);
        tv_time = (TextView)view.findViewById(R.id.tv_time);
        tv_address = (TextView)view.findViewById(R.id.tv_address);
        recyclerView.addHeaderView(view);
    }
    
    public void getData(boolean isLoading){
        JoinShopRequest.getShop(JoinStoresActivity.this,page)
                .compose(DoTransform.<JoinStoreEntity>applyScheduler(this,isLoading))
                .compose(JoinStoresActivity.this.<JoinStoreEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<JoinStoreEntity>(this) {
                    @Override
                    protected void onSuccess(JoinStoreEntity entity) {
                        if(entity.getStatus()==3001){
                            exceptionManager.showEmpty();
                        }else {
                            exceptionManager.hide();
                            
                            if(page==1){
                                notices.clear();

                                tv_shopName.setText(entity.getData().getName());
                                tv_intro.setText(entity.getData().getRemark());
                                tv_time.setText("营业时间："+entity.getData().getOpen_time()+"~"
                                        +entity.getData().getClose_time());
                                tv_address.setText(entity.getData().getAddress());
                                
                                images = (ArrayList<JoinStoreEntity.DataBean.AgentImagesBean>) entity.getData().getAgent_images();
                                banner.setImages(images);
                                banner.setImageLoader(new ImageLoader() {
                                    @Override
                                    public void displayImage(Context context, Object path, ImageView imageView) {
                                        JoinStoreEntity.DataBean.AgentImagesBean agentImagesBean = 
                                                (JoinStoreEntity.DataBean.AgentImagesBean)path;
                                        ImageHelper.display(JoinStoresActivity.this,imageView,
                                                UrlConst.baseUrl()+agentImagesBean.getImage_url());
                                        LogUtil.i("mylog","dd"+UrlConst.baseUrl()+
                                        agentImagesBean.getImage_url());
                                    }
                                }).start();
                                
                            }
                            
                            notices.addAll(entity.getData().getNotice());
                            baseAdapter.setList(notices);
                            
                            if(page>=entity.getPageCount()){
                                smartRefreshLayout.finishLoadMoreWithNoMoreData();
                                tv_.setVisibility(View.VISIBLE);
                            }else {
                                smartRefreshLayout.setNoMoreData(false);
                                tv_.setVisibility(View.GONE);
                            }
                            
                        }
                        
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                    }
                });
                
    }

    @Override
    public void onRefresh() {
    }

    @Override
    public void onLoadMore() {
    }
    

    @Override
    public void onClick(View view) {
        
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        getData(false);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page = page + 1;
        getData(false);
    }
}
